<template>
	<view class="container">
		<view class="content">
			<view class="tabs">
				<view class="tabs-item" v-for="(item, index) in tabs" :key="index"
					:class="{'active': item.value === activeValue}" @click="handleChangeTab(item, index)">
					<text class="tabs-item-text">{{item.label}}</text>
				</view>
			</view>
			<view class="tab-line" :style="{left: tabLineLeft}"></view>
		</view>
	</view>
</template>

<script setup>
	import { ref, onMounted, getCurrentInstance } from 'vue';
	import { useCouponStore } from '../store.js';

	const tabs = ref([{
			label: '未使用',
			value: 2
		},
		{
			label: '已使用',
			value: 3
		},
		{
			label: '已过期',
			value: 4
		}
	]);
	const activeValue = ref(2);
	const tabLineLeft = ref(0);
	const instance = ref(null);
	const store = useCouponStore();

	onMounted(() => {
		instance.value = getCurrentInstance();
		const index = tabs.value.findIndex(item => item.value == activeValue.value);
		const row = tabs.value[index];
		handleChangeTab(row, index);
	});

	const handleChangeTab = (row, index) => {
		activeValue.value = row.value;
		setTabUnderline(index);
		store.SET_STATUS(activeValue.value);
	}

	const setTabUnderline = (index) => {
		uni.createSelectorQuery().in(instance.value).selectAll('.tabs-item-text').boundingClientRect(data => {
			tabLineLeft.value = data[index].left + data[index].width / 2 + 'px';
		}).exec();
	}
</script>

<style lang="scss" scoped>
	$redColor: #FE3333;

	.container {
		height: 90rpx;

		.content {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 9;
			width: 100%;
			height: inherit;
			background-color: white;

			.tabs {
				display: grid;
				grid-template-columns: 1fr 1fr 1fr;
				height: inherit;

				&-item {
					display: flex;
					justify-content: center;
					align-items: center;
					font-size: 28rpx;

					&.active {
						font-size: 30rpx;
						font-weight: bold;
					}
				}
			}

			.tab-line {
				position: absolute;
				bottom: 0;
				left: 0;
				z-index: 9;
				width: 46rpx;
				height: 9rpx;
				background: $redColor;
				border-radius: 5rpx;
				transform: translateX(-50%);
				transition: left 0.2s;
			}
		}
	}
</style>